package message.types.message;

import java.io.IOException;

import message.Message;
import message.MessageParser;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import stalker.Stalker;

public abstract class MessageMessage extends Message {
  
  private final Message message_;
  
  /**
   * Creates a new login success acknowledgement for the corresponding id
   */
  public MessageMessage(Stalker client, Stalker destination, Message message) {
    super(client, destination);
    assert message != null;
    message_ = message;
  }
  
  /**
   * Retrieves this message over the net
   * @throws IOException 
   */
  public MessageMessage(Stalker client, Stalker destination, FrameReader reader) throws IOException {
    super(client, destination);
    assert reader != null;
    message_ = MessageParser.retrieve(reader);
  }
  
  public Message getMessage() {
    return message_;
  }

  public boolean contentEquals(Object obj) {
    if (!(obj instanceof MessageMessage)) {
      return false;
    }
    MessageMessage that = (MessageMessage) obj;
    return this.getClient().equals(that.getClient()) &&
           this.getDestination().equals(that.getDestination()) &&
           this.getMessage().equals(that.getMessage());
  }

  @Override
  public void send(FrameWriter writer) throws IOException {
    MessageParser.send(getMessage(), writer);
  }
  
}
